package model

import "gorm.io/gorm"

type Shop struct {
	gorm.Model
	Name  string `gorm:"type:varchar(20);commit:'商品名称'"`
	Price int    `gorm:"type:int(20);commit:'商品价格'"`
	Num   int    `gorm:"type:int(20);commit:'商品数量'"`
	Stock int    `gorm:"type:int(20);commit:'商品库存'"`
	Img   string `gorm:"type:varchar(500);commit:'商品图片'"`
	State int    `gorm:"type:int;commit:'0-已关注,1-移除关注'"`
}

func (s *Shop) ShopList() error {
	return DB.Where("shop_id=?", s.ID).Find(&s).Error
}
func (s *Shop) CreateAnimals(db *gorm.DB) error {
	// 再唠叨一下，事务一旦开始，你就应该使用 tx 处理数据
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err = tx.Error; err != nil {
		return err
	}

	if err = tx.Create(&s).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
